package com.bwie.bwedu.fragment;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.bwie.bwedu.utils.ShareprefreceUtils;
import com.bwie.bwedu.utils.ToastUtils;

import java.text.SimpleDateFormat;
import java.util.Date;


public abstract class BaseFragment extends Fragment{

	View view;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		if (view == null) {
			view = getView(inflater);
			// 找控件
			findView();

			setData();
			setListener();
		}
		return view;
	}
	//获取布局
	public abstract View getView(LayoutInflater inflater);

	//找控件
	public abstract void findView();

	//设置数据
	public abstract void setData();

	//设置监听
	public abstract void setListener();

	/*
	 * 以指定格式获取当前时间
	 */
	public String getCurrentTime(String format) {
		SimpleDateFormat f = new SimpleDateFormat(format);
		String date = f.format(new Date());
		return date;
	}

	/*
	 * 判断是否是登录状态
	 */
	public boolean isLogin() {
		return !TextUtils.isEmpty(ShareprefreceUtils.readfromExternl("uid",""));
	}

	/*
	 * 弹出吐司
	 */
	public void showToast(String text) {
		if (TextUtils.isEmpty(text))
			return;
		ToastUtils.getToast(text);
	}

	/*
	 * 获取对话框dialog对象
	 */
	public AlertDialog getDialog(Context context, String message,
								 String positive, String negative,
								 DialogInterface.OnClickListener listener) {
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		builder.setIcon(android.R.drawable.ic_dialog_info);
		builder.setTitle("提示");
		builder.setMessage(message);
		builder.setPositiveButton(positive, listener);
		if (!TextUtils.isEmpty(negative)) {
			builder.setNegativeButton(negative, null);
		}
		AlertDialog dialog = builder.create();
		return dialog;
	}

	/*
	 * 弹出指定吐司（网络连接超时）
	 */
	public void showFail() {
		ToastUtils.getToast("网络连接超时...");
	}
}
